package com.stanley.alogrithm.listnode;

import com.stanley.alogrithm.ListNode;

/**
 * 1=》5 =〉9
 *        2 =》8
 * result:
 * 1=〉8 = 》7
 */
public class ListNodePaddingSum {

    private static ListNode build(int[] data) {
        if(data == null) {
            return null;
        }
        ListNode node = null;
        ListNode root = null;
        for (int i = 0; i < data.length; i++) {
            ListNode cur = new ListNode(data[i]);
            if (node != null) {
                node.next = cur;
            }
            node = cur;
            if(i == 0) {
                root = node;
            }
        }

        return root;

    }


    public static void main(String[] args) {

        ListNode root1 = build(new int[]{1, 5, 9});
        ListNode root2 = build(new int[]{2, 8, 3, 4, 6});
        ListNode result = sumListNode(root1, root2);
        System.out.println("result:" + result);

    }

    public static ListNode sumListNode(ListNode root1, ListNode root2) {
        ListNode inverse1 = getInverseNode(root1);
        System.out.println("inverse1:" + inverse1);
        ListNode inverse2 = getInverseNode(root2);
        System.out.println("inverse2:" + inverse2);
        ListNode resultRoot = null;
        ListNode current1 = inverse1, current2 = inverse2;
        boolean lastSumExceedTen = false;
        while (current1 != null || current2 != null) {
            int sum = 0;
            if (current1 != null) {
                sum += current1.value;
                current1 = current1.next;
            }
            if (current2 != null) {
                sum += current2.value;
                current2 = current2.next;
            }
            if (lastSumExceedTen) {
                sum++;
            }
            if (sum >= 10) {
                lastSumExceedTen = true;
                sum -= 10;
            } else {
                lastSumExceedTen = false;
            }

            ListNode pNode = new ListNode(sum);
            if (resultRoot != null) {
                pNode.next = resultRoot;
            }
            resultRoot = pNode;
        }
        if (lastSumExceedTen) {
            ListNode finalNode = new ListNode(1);
            finalNode.next = resultRoot;
            resultRoot = finalNode;
        }
        return resultRoot;
    }

    public static ListNode getInverseNode(ListNode root) {
        int len = 0;
        ListNode inverse = null;
        ListNode current = root;
        while (current != null) {
            len++;
            ListNode previous = inverse;
            inverse = new ListNode(current.value);
            inverse.next = previous;
            current = current.next;
        }
        return inverse;
    }

//    public ListNode sumNext(ListNode node1, ListNode node2, ResultNode result) {
//        ListNode resultNode = null;
//        if (node1.next != null || node2.next != null) {
//            resultNode = sumNext(node1.next, node2.next, result);
//            return resultNode;
//        } else  {
//
//        }
//
//    }
}
